﻿#include "StageControlDockWidget.h"

StageControlDockWidget::StageControlDockWidget(QWidget *parent, Qt::WindowFlags flags)
    :QDockWidget(parent, flags)
{
    initContent();
    initConnection();
    setWindowTitle("Stage Control Dialog");
    //setFeatures(QDockWidget::DockWidgetVerticalTitleBar);
}

void StageControlDockWidget::enableXYControl(bool on)
{
    xyControl->setVisible(on);
    xyPosGB->enableXYPosition(on);
}

void StageControlDockWidget::enableGoHome(bool on)
{
    goHomeGroupBox->setVisible(on);
}

void StageControlDockWidget::setXPosition(double x)
{
    xyPosGB->setXPosition(x);
}

void StageControlDockWidget::setYPosition(double y)
{
    xyPosGB->setYPosition(y);
}

void StageControlDockWidget::setZPosition(double z)
{
    xyPosGB->setZPosition(z);
}

void StageControlDockWidget::setPosition(double x, double y, double z)
{
    xyPosGB->setPosition(x, y, z);
}

void StageControlDockWidget::initContent()
{
    dockWidget = new QWidget(this);
    vLayout = new QVBoxLayout(dockWidget);
    vLayout->setSpacing(6);
    xyControl = new XYControlGroupBox(dockWidget);
    vLayout->addWidget(xyControl);

    goHomeGroupBox = new QGroupBox;
    goHomeLayout = new QVBoxLayout(goHomeGroupBox);
    goHomeLayout->setSpacing(6);
    pushButtonGoHomeX = new QPushButton(goHomeGroupBox);
    pushButtonGoHomeY = new QPushButton(goHomeGroupBox);
    pushButtonGoHomeZ = new QPushButton(goHomeGroupBox);
    pushButtonGoHomeX->setText("X GoHome");
    pushButtonGoHomeY->setText("Y GoHome");
    pushButtonGoHomeZ->setText("Z GoHome");
    goHomeLayout->addWidget(pushButtonGoHomeX);
    goHomeLayout->addWidget(pushButtonGoHomeY);
    goHomeLayout->addWidget(pushButtonGoHomeZ);
    goHomeLayout->addStretch();
    goHomeGroupBox->setLayout(goHomeLayout);
    goHomeGroupBox->setTitle("GoHome");

    horizontalLayout = new QHBoxLayout;
    horizontalLayout->setSpacing(6);
    zControl = new ZControlGroupBox;
    horizontalLayout->addWidget(zControl);
    horizontalLayout->addWidget(goHomeGroupBox);

    vLayout->addLayout(horizontalLayout);


    xyPosGB = new PositionGroupBox;
    vLayout->addWidget(xyPosGB);
    vLayout->addStretch(10);
    dockWidget->setLayout(goHomeLayout);
    setWidget(dockWidget);
}

void StageControlDockWidget::initConnection()
{
    connect(xyControl, SIGNAL(moveForward(double)), this, SIGNAL(moveForward(double)));
    connect(xyControl, SIGNAL(moveBackward(double)), this, SIGNAL(moveBackward(double)));
    connect(xyControl, SIGNAL(moveLeft(double)), this, SIGNAL(moveLeft(double)));
    connect(xyControl, SIGNAL(moveRight(double)), this, SIGNAL(moveRight(double)));
    connect(xyControl, SIGNAL(moveForwardStop()), this, SIGNAL(moveForwardStop()));
    connect(xyControl, SIGNAL(moveBackwardStop()), this, SIGNAL(moveBackwardStop()));
    connect(xyControl, SIGNAL(moveLeftStop()), this, SIGNAL(moveLeftStop()));
    connect(xyControl, SIGNAL(moveRightStop()), this, SIGNAL(moveRightStop()));
    connect(xyControl, SIGNAL(stop()), this, SIGNAL(stopXY()));

    connect(zControl, SIGNAL(moveUp(double)), this, SIGNAL(moveUp(double)));
    connect(zControl, SIGNAL(moveDown(double)), this, SIGNAL(moveDown(double)));
    connect(zControl, SIGNAL(moveUpStop()), this, SIGNAL(moveUpStop()));
    connect(zControl, SIGNAL(moveDownStop()), this, SIGNAL(moveDownStop()));
    connect(zControl, SIGNAL(stop()), this, SIGNAL(stopZ()));
    connect(zControl, SIGNAL(autoFocus()), this, SIGNAL(autoFocus()));

    connect(xyControl, SIGNAL(changeSpeed(double)), this, SIGNAL(changeXYSpeed(double)));
    connect(zControl, SIGNAL(changeSpeed(double)), this, SIGNAL(changeZSpeed(double)));

    connect(pushButtonGoHomeX, SIGNAL(clicked(bool)), this, SIGNAL(goHomeX()));
    connect(pushButtonGoHomeY, SIGNAL(clicked(bool)), this, SIGNAL(goHomeY()));
    connect(pushButtonGoHomeZ, SIGNAL(clicked(bool)), this, SIGNAL(goHomeZ()));

}

void StageControlDockWidget::xySpeedUp()
{
    xyControl->speedUp();
}

void StageControlDockWidget::xySpeedDown()
{
    xyControl->speedDown();
}

void StageControlDockWidget::zSpeedUp()
{
    zControl->speedUp();
}

void StageControlDockWidget::zSpeedDown()
{
    zControl->speedDown();
}

void StageControlDockWidget::joystick_moveUp()
{
    double spd = zControl->speed();
    emit moveUp(spd);
}

void StageControlDockWidget::joystick_moveDown()
{
    double spd = zControl->speed();
    emit moveDown(spd);
}

void StageControlDockWidget::joystick_moveForward()
{
    double spd = xyControl->speed();
    emit moveForward(spd);
}

void StageControlDockWidget::joystick_moveBackward()
{
    double spd = xyControl->speed();
    emit moveBackward(spd);
}

void StageControlDockWidget::joystick_moveLeft()
{
    double spd = xyControl->speed();
    emit moveLeft(spd);
}

void StageControlDockWidget::joystick_moveRight()
{
    double spd = xyControl->speed();
    emit moveRight(spd);
}
